#include <iostream>
using namespace std;

bool isleapyear(int year) {
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
        return true;
    else return false;
}

int getmonthday(int year, int month) {
    int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if (isleapyear(year)) {
        arr[2] = 29;
    }
    return arr[month];
}

int main() {
    int m = 0;
    cin >> m;
    int year, month, day, n;
    while (m--) {
        cin >> year >> month >> day >> n;
        day += n;
        while (day > getmonthday(year, month)) {
            day -= getmonthday(year, month);
            month++;
            if (month == 13) {
                month = 1;
                year++;
            }
        }
        printf("%04d-%02d-%02d\n", year, month, day);
    }
    return 0;
}